package com.qianclass.talklive.app.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;

import com.google.gson.Gson;
import com.qianclass.talklive.app.InterfaceBack.OnDrawTouchListener;
import com.qianclass.talklive.app.InterfaceBack.OnWebClickListener;
import com.qianclass.talklive.app.bean.AddGraffitiBean;
import com.qianclass.talklive.app.bean.GraffitiInfo;
import com.qianclass.talklive.app.bean.GraffitiShapeBean;
import com.qianclass.talklive.app.utils.ColorUtils;
import com.qianclass.talklive.app.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2017/12/15 0015.
 */

public class DrawableWebView extends WebView {

    private Paint mPaint;
    private int mDrawSize;
    private float mLastX;
    private float mLastY;
    private Path mPath;
    private Bitmap mBufferBitmap;
    private Canvas mBufferCanvas;
    private float mLastDistance;
    private boolean isSingleFinger;
    private Matrix mBufferMatrix;
    private float mTouchLastX;
    private float mTouchLastY;
    private Context mContext;
    private double mScale;
    private int mFileID;
    private int mCurrentPage;
    private List<GraffitiInfo.GraffitiPageBean.GraffitiDataBean.CoordinatesBean> mCoordinatesBeanLists;
    private List<GraffitiInfo.GraffitiPageBean.GraffitiDataBean> mGraffitiDataBeanList;
    private List<GraffitiInfo> mGraffitiInfoList;
    private float mContentTop;
    private float mContentBottom;
    private float mContentSacle;
    private List<GraffitiInfo.GraffitiPageBean.GraffitiDataBean> mRevokeGraffitiDataLists;
    private OnDrawTouchListener mOnDrawTouchListener;
    private int mViewHeight;
    private int mViewWidth;
    private float mContentLeft;
    private float mContentRight;
    private boolean mIsInvalidDown;
    private OnWebClickListener mOnWebClickListener;
    private boolean isWebClicked;
    private float mClickDownX;
    private float mClickDownY;

    /**
     * 控件模式
     */
    public enum Mode {
        DRAW,
        SCALE
    }

    /**
     * 画板位置模式
     */
    public enum LocationMode {
        TOP,
        CENTER,
        BOTTOM
    }

    /**
     * 涂鸦笔迹类型
     */
    public String SHAPE_PEN = "pen";
    public String SHAPE_LINE = "line";
    public String SHAPE_RECT = "rect";
    public String SHAPE_OVAL = "oval";
    public String SHAPE_TEXT = "text";
    public String PENCOLOR = "#FF9000";

    private Mode mMode = Mode.SCALE;

    public DrawableWebView(Context context) {
        super(context);
        init();
        mContext = context;
    }

    public DrawableWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        mContext = context;
    }

    public DrawableWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        mContext = context;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public DrawableWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
        mContext = context;
    }

    public DrawableWebView(Context context,
                           AttributeSet attrs,
                           int defStyleAttr,
                           boolean privateBrowsing)
    {
        super(context, attrs, defStyleAttr, privateBrowsing);
        init();
        mContext = context;
    }

    private void init() {
        //创建画笔对象
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setFilterBitmap(true);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mDrawSize = 3;
        mPaint.setStrokeWidth(mDrawSize);
        mPaint.setColor(Color.parseColor(PENCOLOR));

        //存储不同fileID笔迹的集合
        if (mGraffitiInfoList == null) {
            mGraffitiInfoList = new ArrayList<>();
        }

        //存储每条笔迹的集合
        if (mGraffitiDataBeanList == null) {
            mGraffitiDataBeanList = new ArrayList<>();
        }

        //撤销笔迹的集合，用于储存当前页中撤销的笔迹
        if (mRevokeGraffitiDataLists == null) {
            mRevokeGraffitiDataLists = new ArrayList<>();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        mViewHeight = getMeasuredHeight();
        mViewWidth = getMeasuredWidth();
    }

    public void initData(int fileID, int currentPage, int webContentWidth, int webContentHeight, LocationMode locationMode) {

        changePage(fileID, currentPage);
        mFileID = fileID;
        mCurrentPage = currentPage;

        if (webContentWidth != 0 && webContentHeight != 0) {

            //宽度缩放
            float widthSacle = mViewWidth / (webContentWidth * 1.0f);
            //高度缩放
            float heightSacle = mViewHeight / (webContentHeight * 1.0f);

            if (locationMode == LocationMode.CENTER) {

                if (widthSacle > heightSacle) {
                    //内容横向有空隙
                    mContentSacle = heightSacle;
                    float spaceW = (mViewHeight * webContentWidth) / (webContentHeight * 1.0f);

                    mContentLeft = (mViewWidth - spaceW) / 2.0f;
                    mContentTop = 0;
                    mContentRight = mViewWidth - mContentLeft;
                    mContentBottom = mViewHeight;

                } else if (widthSacle == heightSacle) {
                    //内容无空隙
                    mContentSacle = widthSacle;

                    mContentLeft = 0;
                    mContentTop = 0;
                    mContentRight = mViewWidth;
                    mContentBottom = mViewHeight;
                } else {
                    //内容纵向有空隙
                    mContentSacle = widthSacle;
                    float spaceH = (mViewWidth * webContentHeight) / (webContentWidth * 1.0f);

                    mContentLeft = 0;
                    mContentTop = (mViewHeight - spaceH) / 2.0f;
                    mContentRight = mViewWidth;
                    mContentBottom = mViewHeight - mContentTop;
                }
            } else if(locationMode == LocationMode.TOP){
                //内容无空隙
                mContentSacle = widthSacle;

                mContentLeft = 0;
                mContentTop = 0;
                mContentRight = mViewWidth;
                mContentBottom = mViewHeight;
            }

            LogUtil.e("move", "mContentSacle:" + mContentSacle + " left:" + mContentLeft + " top:" + mContentTop + " right:" + mContentRight + " bottom:" + mContentBottom);
        }
    }

    public void initData(int fileID, int currentPage, int webContentWidth, int webContentHeight) {

        initData(fileID, currentPage, webContentWidth, webContentHeight, LocationMode.CENTER);

        /*changePage(fileID, currentPage);
        mFileID = fileID;
        mCurrentPage = currentPage;

        if (webContentWidth != 0 && webContentHeight != 0) {
            //宽度缩放
            float widthSacle = mViewWidth / (webContentWidth * 1.0f);
            //高度缩放
            float heightSacle = mViewHeight / (webContentHeight * 1.0f);

            if (widthSacle > heightSacle) {
                //内容横向有空隙
                mContentSacle = heightSacle;
                float spaceW = (mViewHeight * webContentWidth) / (webContentHeight * 1.0f);

                mContentLeft = (mViewWidth - spaceW) / 2.0f;
                mContentTop = 0;
                mContentRight = mViewWidth - mContentLeft;
                mContentBottom = mViewHeight;

            } else if (widthSacle == heightSacle) {
                //内容无空隙
                mContentSacle = widthSacle;

                mContentLeft = 0;
                mContentTop = 0;
                mContentRight = mViewWidth;
                mContentBottom = mViewHeight;
            } else {
                //内容纵向有空隙
                mContentSacle = widthSacle;
                float spaceH = (mViewWidth * webContentHeight) / (webContentWidth * 1.0f);

                mContentLeft = 0;
                mContentTop = (mViewHeight - spaceH) / 2.0f;
                mContentRight = mViewWidth;
                mContentBottom = mViewHeight - mContentTop;

            }
            LogUtil.e("move", "mContentSacle:" + mContentSacle + " left:" + mContentLeft + " top:" + mContentTop + " right:" + mContentRight + " bottom:" + mContentBottom);

        }*/
    }

    public Mode getMode() {
        return mMode;
    }

    public void setMode(Mode mode) {
        if (mode != mMode) {
            mMode = mode;
            if (mMode == Mode.DRAW) {
                mPaint.setXfermode(null);
                mPaint.setStrokeWidth(mDrawSize);
            } else {
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mBufferBitmap != null) {
            canvas.drawBitmap(mBufferBitmap, mBufferMatrix, null);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        final int x = (int) event.getX();
        final int y = (int) event.getY();

        if (mMode == Mode.SCALE) {
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    mClickDownX = x;
                    mClickDownY = y;
                    isWebClicked = true;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (Math.abs(x - mClickDownX) < 16 && Math.abs(y - mClickDownY) < 16) {
                        isWebClicked = true;
                    } else {
                        isWebClicked = false;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (isWebClicked && mOnWebClickListener != null) {
                        mOnWebClickListener.onWebClick();
                    }
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                case MotionEvent.ACTION_POINTER_UP:
                    return true;
            }
            return super.onTouchEvent(event);
        } else {
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    if (mPath == null) {
                        mPath = new Path();
                    }
                    if (y >= mContentTop && y <= mContentBottom && x >= mContentLeft && x <= mContentRight) {
                        mLastX = x;
                        mLastY = y;
                        mPaint.setColor(Color.parseColor(PENCOLOR));

                        mPath.moveTo(x, y);
                    } else {
                        //无效落点
                        mIsInvalidDown = true;
                    }

                    break;
                case MotionEvent.ACTION_MOVE:
                    //这里终点设为两点的中心点的目的在于使绘制的曲线更平滑，如果终点直接设置为x,y，效果和lineto是一样的,实际是折线效果
                    if (y >= mContentTop && y <= mContentBottom && x >= mContentLeft && x <= mContentRight) {
                        LogUtil.e("move", "x:" + x + " y:" + y);
                        if (mIsInvalidDown) {
                            mLastX = x;
                            mLastY = y;
                            mPaint.setColor(Color.parseColor(PENCOLOR));

                            mPath.moveTo(x, y);
                            mIsInvalidDown = false;
                        }

                        mPath.quadTo(mLastX, mLastY, (x + mLastX) / 2, (y + mLastY) / 2);
                        if (mBufferBitmap == null) {
                            mBufferBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
                            mBufferCanvas = new Canvas(mBufferBitmap);
                            mBufferMatrix = new Matrix();
                            mBufferCanvas.drawColor(Color.TRANSPARENT);
                        }
                        mBufferCanvas.drawPath(mPath, mPaint);
                        invalidate();

                        if (mCoordinatesBeanLists == null) {
                            mCoordinatesBeanLists = new ArrayList<>();
                        }
                        mCoordinatesBeanLists.add(new GraffitiInfo.GraffitiPageBean.GraffitiDataBean.CoordinatesBean((int)((x - mContentLeft) / mContentSacle), (int)((y - mContentTop) / mContentSacle)));
                        mLastX = x;
                        mLastY = y;
                    }

                    break;
                case MotionEvent.ACTION_UP:
                    mPath.reset();

                    if (mCoordinatesBeanLists != null) {
                        GraffitiInfo.GraffitiPageBean.GraffitiDataBean graffitiDataBean = new GraffitiInfo.GraffitiPageBean.GraffitiDataBean();
                        List<GraffitiInfo.GraffitiPageBean.GraffitiDataBean.CoordinatesBean> coordinatesBeanList = new ArrayList<>();
                        if (mCoordinatesBeanLists.size() > 200) {
                            float proportion = mCoordinatesBeanLists.size() / 200.0f;
                            for (int i = 0; i < 200; i++) {
                                coordinatesBeanList.add(mCoordinatesBeanLists.get((int) (i * proportion)));
                            }
                        } else {
                            for (GraffitiInfo.GraffitiPageBean.GraffitiDataBean.CoordinatesBean beanList : mCoordinatesBeanLists) {
                                coordinatesBeanList.add(beanList);
                            }
                        }

                        graffitiDataBean.setCoordinates(coordinatesBeanList);

                        int graffitiId = Math.abs(new Random().nextInt());

                        graffitiDataBean.setGraffitiId(graffitiId);
                        graffitiDataBean.setGraffitiShape(SHAPE_PEN);
                        graffitiDataBean.setGraffitiColor(PENCOLOR);

                        //存储每条笔迹的集合
                        if (mGraffitiDataBeanList == null) {
                            mGraffitiDataBeanList = new ArrayList<>();
                        }

                        mOnDrawTouchListener.onDrawData(graffitiDataBean);

                        mGraffitiDataBeanList.add(graffitiDataBean);
                        mCoordinatesBeanLists.clear();
                        mRevokeGraffitiDataLists.clear();
                    }
                    break;
            }

            return true;
        }
    }

    /**
     * 触摸划线数据的监听器
     * @param onDrawTouchListener
     */
    public void setOnDrawTouchListener(OnDrawTouchListener onDrawTouchListener) {
        mOnDrawTouchListener = onDrawTouchListener;
    }

    /**
     * 点击的监听
     * @param onWebClickListener
     */
    public void setOnWebClickListener(OnWebClickListener onWebClickListener) {
        mOnWebClickListener = onWebClickListener;
    }

    /**
     * 页面切换
     * @param fileID
     * @param currentPage
     */
    public void changePage(int fileID, int currentPage){
        LogUtil.e("changePage0", "fileID" + fileID + ",mFileID" + mFileID);
        if(mFileID != 0 && (mFileID != fileID || mCurrentPage != currentPage)){
            boolean isDataExist = false;

            LogUtil.e("changePage1", "fileID" + fileID + ",mFileID" + mFileID);

            clearCanvs();

            GraffitiInfo graffitiInfos = null;
            List<GraffitiInfo.GraffitiPageBean> graffitiPageBeanList = null;

            //清空笔迹撤销的集合
            mRevokeGraffitiDataLists.clear();

            for (GraffitiInfo graffitiInfo : mGraffitiInfoList) {
                if (graffitiInfo.getFileId() == mFileID) {

                    graffitiInfos = graffitiInfo;
                    graffitiPageBeanList = graffitiInfo.getGraffitiPage();
                    for (GraffitiInfo.GraffitiPageBean graffitiPageBean : graffitiPageBeanList) {
                        if (mCurrentPage == graffitiPageBean.getCurrentPage()) {
                            List<GraffitiInfo.GraffitiPageBean.GraffitiDataBean> graffitiDataBeanList = new ArrayList<>();
                            for (GraffitiInfo.GraffitiPageBean.GraffitiDataBean graffitiDataBean : mGraffitiDataBeanList) {
                                graffitiDataBeanList.add(graffitiDataBean);
                            }
                            graffitiPageBean.setGraffitiData(graffitiDataBeanList);
                            mGraffitiDataBeanList.clear();
                            isDataExist = true;
                        }
                    }
                }
            }

            if (!isDataExist) {
                List<GraffitiInfo.GraffitiPageBean.GraffitiDataBean> graffitiDataBeanList = new ArrayList<>();
                for (GraffitiInfo.GraffitiPageBean.GraffitiDataBean graffitiDataBean : mGraffitiDataBeanList) {
                    graffitiDataBeanList.add(graffitiDataBean);
                }
                GraffitiInfo.GraffitiPageBean mGraffitiPageBean = new GraffitiInfo.GraffitiPageBean();
                mGraffitiPageBean.setCurrentPage(mCurrentPage);
                mGraffitiPageBean.setGraffitiData(graffitiDataBeanList);
                mGraffitiDataBeanList.clear();

                if (graffitiPageBeanList == null) {
                    graffitiPageBeanList = new ArrayList<>();
                }

                graffitiPageBeanList.add(mGraffitiPageBean);
            }

            if (graffitiInfos == null) {
                graffitiInfos = new GraffitiInfo();
                graffitiInfos.setFileId(mFileID);

                mGraffitiInfoList.add(graffitiInfos);
            }

            graffitiInfos.setGraffitiPage(graffitiPageBeanList);

            LogUtil.e("json", new Gson().toJson(mGraffitiInfoList));

            mFileID = fileID;
            mCurrentPage = currentPage;
            for (GraffitiInfo graffitiInfo : mGraffitiInfoList) {
                if (graffitiInfo.getFileId() == mFileID) {
                    graffitiPageBeanList = graffitiInfo.getGraffitiPage();
                    for (GraffitiInfo.GraffitiPageBean graffitiPageBean : graffitiPageBeanList) {
                        if (mCurrentPage == graffitiPageBean.getCurrentPage()) {
                            mGraffitiDataBeanList = graffitiPageBean.getGraffitiData();
                        }
                    }
                }
            }

            postDelayed(new Runnable() {
                @Override
                public void run() {
                    restoreCanvs();
                }
            }, 10);
        }
    }

    /**
     * 清空画布Canvas
     */
    public void clearCanvs(){
        if (mBufferCanvas != null && mPaint != null && mPath != null) {
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            mBufferCanvas.drawPaint(mPaint);
            mPaint.setXfermode(null);
            mPath.reset();

            invalidate();
        }
    }

    /**
     * 清除笔迹
     * @param fileID
     * @param currentPage
     */
    public void clearGraffiti(int fileID, int currentPage){
        for (GraffitiInfo graffitiInfo : mGraffitiInfoList) {
            if (fileID == graffitiInfo.getFileId()) {
                for (GraffitiInfo.GraffitiPageBean graffitiPageBean : graffitiInfo.getGraffitiPage()) {
                    if (currentPage == graffitiPageBean.getCurrentPage()) {
                        graffitiPageBean.getGraffitiData().clear();
                    }
                }
            }
        }
        mGraffitiDataBeanList.clear();
        mRevokeGraffitiDataLists.clear();

        clearCanvs();
    }

    /**
     * 撤销笔迹
     */
    public int revokeGraffiti() {
        int graffitiid = 0;
        if (mGraffitiDataBeanList.size() > 0) {
            clearCanvs();
            graffitiid = mGraffitiDataBeanList.get(mGraffitiDataBeanList.size() - 1)
                                              .getGraffitiId();
            mRevokeGraffitiDataLists.add(mGraffitiDataBeanList.get(mGraffitiDataBeanList.size() - 1));
            mGraffitiDataBeanList.remove(mGraffitiDataBeanList.size() - 1);

            restoreCanvs();
        }

        return graffitiid;
    }

    public void revokeGraffiti(int graffitiId){
        if (mGraffitiDataBeanList.size() > 0) {
            clearCanvs();
            /*for (GraffitiInfo.GraffitiPageBean.GraffitiDataBean graffitiDataBean : mGraffitiDataBeanList) {
                if (graffitiId == graffitiDataBean.getGraffitiId()) {
                    mRevokeGraffitiDataLists.add(graffitiDataBean);
                    mGraffitiDataBeanList.remove(graffitiDataBean);
                }
            }*/
            for (int i = 0; i < mGraffitiDataBeanList.size(); i++) {
                GraffitiInfo.GraffitiPageBean.GraffitiDataBean graffitiDataBean = mGraffitiDataBeanList.get(i);
                if (graffitiId == graffitiDataBean.getGraffitiId()) {
                    mRevokeGraffitiDataLists.add(graffitiDataBean);
                    mGraffitiDataBeanList.remove(graffitiDataBean);
                }
            }

            restoreCanvs();
        }
    }

    /**
     * 恢复笔迹
     */
    public AddGraffitiBean restoreGraffiti() {
        AddGraffitiBean addGraffitiBean = null;
        if (mRevokeGraffitiDataLists.size() > 0) {
            clearCanvs();
            GraffitiInfo.GraffitiPageBean.GraffitiDataBean graffitiDataBean = mRevokeGraffitiDataLists.get(mRevokeGraffitiDataLists.size() - 1);
            int graffitiId = graffitiDataBean.getGraffitiId();
            String graffitiColor = graffitiDataBean.getGraffitiColor();
            String shape = graffitiDataBean.getGraffitiShape();
            String graffitiText = graffitiDataBean.getGraffitiText();
            List<GraffitiInfo.GraffitiPageBean.GraffitiDataBean.CoordinatesBean> coordinates = graffitiDataBean
                    .getCoordinates();


            addGraffitiBean = new AddGraffitiBean();
            AddGraffitiBean.MessageBean messageBean = new AddGraffitiBean.MessageBean();
            AddGraffitiBean.MessageBean.WhiteboardBean whiteboardBean = new AddGraffitiBean.MessageBean.WhiteboardBean();
            AddGraffitiBean.MessageBean.WhiteboardBean.RectBean rectBean = new AddGraffitiBean.MessageBean.WhiteboardBean.RectBean();
            if (shape.equals(SHAPE_PEN)) {
                whiteboardBean.setObjdata(coordinates);
            } else {
                rectBean.setLeft(coordinates.get(0).getX());
                rectBean.setTop(coordinates.get(0).getY());
                rectBean.setRight(coordinates.get(1).getX());
                rectBean.setBottom(coordinates.get(1).getY());
            }
            whiteboardBean.setFileid(mFileID);
            whiteboardBean.setCurrentpage(mCurrentPage);
            whiteboardBean.setColour(graffitiColor);
            whiteboardBean.setObjid(graffitiId);
            whiteboardBean.setSubcommand("addgraffiti");
            whiteboardBean.setObjtype(shape);
            whiteboardBean.setString(graffitiText);
            whiteboardBean.setRect(rectBean);
            messageBean.setType("whiteboard");
            messageBean.setWhiteboard(whiteboardBean);
            addGraffitiBean.setMessage(messageBean);

            mGraffitiDataBeanList.add(graffitiDataBean);
            mRevokeGraffitiDataLists.remove(mRevokeGraffitiDataLists.size() - 1);
            restoreCanvs();
        }
        return addGraffitiBean;
    }

    /**
     * 恢复画板
     */
    private void restoreCanvs(){
        try {
            if (mPaint == null) {
                init();
            }

            if (mBufferBitmap == null) {
                mBufferBitmap = Bitmap.createBitmap(getWidth(), getHeight(),
                                                    Bitmap.Config.ARGB_8888);
                mBufferCanvas = new Canvas(mBufferBitmap);
                mBufferMatrix = new Matrix();
            }
            mBufferCanvas.drawColor(Color.TRANSPARENT);

            for (GraffitiInfo.GraffitiPageBean.GraffitiDataBean graffitiDataBean : mGraffitiDataBeanList) {
                String graffitiShape = graffitiDataBean.getGraffitiShape();
                mPaint.setColor(Color.parseColor(ColorUtils.chageColor(graffitiDataBean.getGraffitiColor())));
                if (graffitiShape == null) {
                    return;
                }
                if (graffitiShape.equals(SHAPE_PEN)) {
                    List<GraffitiInfo.GraffitiPageBean.GraffitiDataBean.CoordinatesBean> coordinates = graffitiDataBean
                            .getCoordinates();
                    LogUtil.e("pointsize", "pointsize:" + coordinates.size());
                    if (coordinates != null && coordinates.size() > 0) {
                        if (coordinates.size() > 0) {
                            mPath.moveTo(coordinates.get(0).getX(), coordinates.get(0).getY());
                            for (int i = 0; i < coordinates.size(); i++) {
                                if (i == 0) {
                                    mLastX = coordinates.get(i).getX() * mContentSacle + mContentLeft;
                                    mLastY = coordinates.get(i).getY() * mContentSacle + mContentTop;
                                    mPath.moveTo(mLastX, mLastY);
                                    mBufferCanvas.drawPoint(mLastX, mLastY, mPaint);
                                    invalidate();

                                } else {
                                    float x = coordinates.get(i).getX() * mContentSacle + mContentLeft;
                                    float y = coordinates.get(i).getY() * mContentSacle + mContentTop;
                                    mPath.quadTo(mLastX, mLastY, (x + mLastX) / 2, (y + mLastY) / 2);
                                    mBufferCanvas.drawPath(mPath, mPaint);
                                    invalidate();
                                    mLastX = x;
                                    mLastY = y;
                                }
                            }
                        }
                    }
                    mPath.reset();
                } else {
                    List<GraffitiInfo.GraffitiPageBean.GraffitiDataBean.CoordinatesBean> coordinates = graffitiDataBean.getCoordinates();
                    mPaint.setColor(Color.parseColor(ColorUtils.chageColor(graffitiDataBean.getGraffitiColor())));
                    graffitiDataBean.getGraffitiColor();
                    if (coordinates.size() == 2) {
                        float x0 = coordinates.get(0).getX() * mContentSacle + mContentLeft;
                        float y0 = coordinates.get(0).getY() * mContentSacle + mContentTop;
                        float x1 = coordinates.get(1).getX() * mContentSacle + mContentLeft;
                        float y1 = coordinates.get(1).getY() * mContentSacle + mContentTop;

                        if (graffitiShape.equals(SHAPE_LINE)) {
                            mPath.moveTo(x0, y0);
                            mPath.lineTo(x1, y1);
                        } else if (graffitiShape.equals(SHAPE_RECT)) {
                            mPath.addRect(x0, y0, x1, y1, Path.Direction.CCW);
                        }else if(graffitiShape.equals(SHAPE_OVAL)){
                            RectF rectF = new RectF(x0, y0, x1, y1);
                            mPath.addOval(rectF, Path.Direction.CCW);
                        }else if (graffitiShape.equals(SHAPE_TEXT)) {
                            mPaint.setTextSize(40);
                            mPaint.setTextAlign(Paint.Align.LEFT);
                            mPaint.setStyle(Paint.Style.FILL);
                            mBufferCanvas.drawText(graffitiDataBean.getGraffitiText(), x0, y0, mPaint);
                            mPaint.setStyle(Paint.Style.STROKE);
                        }

                        mBufferCanvas.drawPath(mPath, mPaint);
                        invalidate();
                        mPath.reset();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void drawGraffiti(GraffitiInfo.GraffitiPageBean.GraffitiDataBean graffitiDataBean){
        //存储每条笔迹的集合
        graffitiDataBean.setGraffitiShape(SHAPE_PEN);
        mGraffitiDataBeanList.add(graffitiDataBean);

        LogUtil.e("drawGraffiti", new Gson().toJson(mGraffitiDataBeanList));
        if (mBufferBitmap == null) {
            mBufferBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            mBufferCanvas = new Canvas(mBufferBitmap);
            mBufferMatrix = new Matrix();
        }
        if (mPath == null) {
            mPath = new Path();
        }
        Path path = new Path();
        float lastX = 0;
        float lastY = 0;
        List<GraffitiInfo.GraffitiPageBean.GraffitiDataBean.CoordinatesBean> coordinates = graffitiDataBean.getCoordinates();
        LogUtil.e("pointsize", "pointsize:" + coordinates.size());
        if (coordinates != null && coordinates.size() > 0) {
            if (coordinates.size() > 0) {
                mPaint.setColor(Color.parseColor(ColorUtils.chageColor(graffitiDataBean.getGraffitiColor())));
                for (int i = 0; i < coordinates.size(); i++) {
                    if (i == 0) {
                        lastX = coordinates.get(i).getX() * mContentSacle + mContentLeft;
                        lastY = coordinates.get(i).getY() * mContentSacle + mContentTop;
                        if (coordinates.size() == 1) {
                            mBufferCanvas.drawPoint(lastX, lastY, mPaint);
                        } else {
                            path.moveTo(lastX, lastY);
                        }

                        invalidate();
                    } else {
                        float x = coordinates.get(i).getX() * mContentSacle + mContentLeft;
                        float y = coordinates.get(i).getY() * mContentSacle + mContentTop;
                        path.quadTo(lastX, lastY, (x + lastX) / 2, (y + lastY) / 2);
                        mBufferCanvas.drawPath(path, mPaint);
                        invalidate();
                        lastX = x;
                        lastY = y;
                    }
                }
            }
            path.reset();
        }
    }

    /**
     * 绘制涂鸦形状
     * @param graffitiId
     * @param graffitiShapeBean
     */
    public void drawGraffitiShape(int graffitiId, GraffitiShapeBean graffitiShapeBean){
        String shape = graffitiShapeBean.getShape();
        if (mBufferBitmap == null) {
            mBufferBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            mBufferCanvas = new Canvas(mBufferBitmap);
            mBufferMatrix = new Matrix();
        }
        if (mPath == null) {
            mPath = new Path();
        }
        mBufferCanvas.drawColor(Color.TRANSPARENT);
        mPaint.setColor(Color.parseColor(ColorUtils.chageColor(graffitiShapeBean.getColor())));

        float left = graffitiShapeBean.getLeft() * mContentSacle + mContentLeft;
        float top = graffitiShapeBean.getTop() * mContentSacle + mContentTop;
        float right = graffitiShapeBean.getRight() * mContentSacle + mContentLeft;
        float bottom = graffitiShapeBean.getBottom() * mContentSacle + mContentTop;

        if (shape.equals(SHAPE_LINE)) {
            //mBufferCanvas.drawLine(left, top, right, bottom, mPaint);
            mPath.moveTo(left, top);
            mPath.lineTo(right, bottom);
        } else if (shape.equals(SHAPE_RECT)) {
            mPath.addRect(left, top, right, bottom, Path.Direction.CCW);
        }else if(shape.equals(SHAPE_OVAL)){
            RectF rectF = new RectF(left, top, right, bottom);
            mPath.addOval(rectF, Path.Direction.CCW);
        }else if (shape.equals(SHAPE_TEXT)) {
            mPaint.setTextSize(40);
            mPaint.setTextAlign(Paint.Align.LEFT);
            mPaint.setStyle(Paint.Style.FILL);
            mBufferCanvas.drawText(graffitiShapeBean.getText(), left, top, mPaint);
            mPaint.setStyle(Paint.Style.STROKE);
        }

        mBufferCanvas.drawPath(mPath, mPaint);
        invalidate();
        mPath.reset();

        GraffitiInfo.GraffitiPageBean.GraffitiDataBean graffitiDataBean = new GraffitiInfo.GraffitiPageBean.GraffitiDataBean();
        graffitiDataBean.setGraffitiShape(shape);
        graffitiDataBean.setGraffitiId(graffitiId);
        graffitiDataBean.setGraffitiColor(graffitiShapeBean.getColor());
        if (shape.equals(SHAPE_TEXT)) {
            graffitiDataBean.setGraffitiText(graffitiShapeBean.getText());
        }
        List<GraffitiInfo.GraffitiPageBean.GraffitiDataBean.CoordinatesBean> coordinatesBeanList = new ArrayList<>();
        coordinatesBeanList.add(new GraffitiInfo.GraffitiPageBean.GraffitiDataBean.CoordinatesBean(graffitiShapeBean.getLeft(), graffitiShapeBean.getTop()));
        coordinatesBeanList.add(new GraffitiInfo.GraffitiPageBean.GraffitiDataBean.CoordinatesBean(graffitiShapeBean.getRight(), graffitiShapeBean.getBottom()));
        graffitiDataBean.setCoordinates(coordinatesBeanList);
        mGraffitiDataBeanList.add(graffitiDataBean);
    }
}
